import java.io.File;
import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) {
        // 输入信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要搜索的目录：");
        String rootPath = sc.next();
        System.out.println("请输入要查询的词：");
        String searchWord = sc.next();

        //构造file对象看是否合法
        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()){
            System.out.println("输入的搜索目录非法！");
            return;
        }

        searchFile(rootFile,searchWord);
    }

    private static void searchFile(File rootFile, String searchWord) {
        File[] files = rootFile.listFiles();
        if(files == null) return;
        for(File f : files){
            if(f.isFile()){
                String fileName = f.getName();
                if (fileName.contains(searchWord)){
                    System.out.println("找到匹配结果：" + f.getAbsoluteFile());
                }
            }else{
                searchFile(f,searchWord);
            }
        }
    }
}
